<template>
	<view class="container">
		<navSearchBar title="我的购物车"></navSearchBar>
		<scroll-view class="scroll" scroll-y @scrolltolower="loadMore">
			<view class="list">
				<uni-swipe-action>
					<view class="" v-for="(item,index) in list" :key="index">
						<uni-swipe-action-item :autoClose="true">
							<view class="list-item flex-row-center">
								<image :src="item.product.picture" mode=""></image>
								<text>{{item.product.title}}</text>
							</view>
							<template v-slot:right>
								<view class="action-btn" @click.stop="handleDel(item.id)">删除</view>
							</template>
						</uni-swipe-action-item>
					</view>
				</uni-swipe-action>
			</view>
		</scroll-view>
		<button class="form-btn button" @click="handleCreat">一键生成PPT</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				page:1,
				pageSize:10
			};
		},
		onLoad() {
			this.getCarList()
		},
		methods: {
			async getCarList(limit) {
				uni.showLoading({title: '加载中'})
				//没有分页
				let res = await this.$api.carList({limit:limit?limit:this.page * this.pageSize})
				if (res.data.status) {
					uni.hideLoading();
					this.list = res.data.data
				}
			},
			loadMore() {
				if (this.list.length <= this.page * this.pageSize) return
				this.page++
				this.getCarList()
			},
			async handleDel(id) {
				uni.showLoading()
				let res = await this.$api.delCar({id})
				uni.hideLoading();
				if (res.data.status == 200) {
					this.$tip.success("删除成功")
					this.getCarList(this.list.lenght-1)
				} else {
					this.$tip.error("删除失败")
				}
			},
			handleCreat() {
				if(this.list.length > 0){
					this.$Router.push({name: "carPpt"})
				} else {
					this.$tip.toast('该购物车无数据')
				}
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		display: flex;
		flex-direction: column;
		height: 100vh;
	}

	.scroll {
		flex: 1;
		height: 100%;
		overflow: hidden;
	}

	.list {
		padding: 10rpx 30rpx 60rpx;

		.list-item {
			padding: 30rpx 0;
			border-bottom: 2rpx solid #EFEFEF;

			image {
				width: 140rpx;
				height: 100rpx;
				border-radius: 20rpx;
				margin-right: 30rpx;
			}

			text {
				font-size: 32rpx;
				font-weight: Semibold;
				color: #525252;
			}
		}

		.action-btn {
			width: 240rpx;
			height: 164rpx;
			line-height: 164rpx;
			text-align: center;
			color: #FFFFFF;
			font-size: 32rpx;
			background: #FF3D3D;
		}
	}

	.button {
		--btn-width: 282rpx;
		margin-bottom: 40rpx;
	}
</style>